<?php
namespace app\push\model;
use think\Model;
use think\Db;
use think\Config;
class DashboardV2 extends Model
{
    /**
     *采集基础数据
     *@return array or fals
     */
    public static function base() {
        !self::cpuPercent()?:$data['cpuPercent'] = self::cpuPercent();
        if($data){
            return $data;
        }else{
            return false;
        }
    }
    
    /*
     *返回cpu使用率
     *@runtime 0.5s
     *@return num or false
     */
    public static function  cpuPercent() {
        preg_match_all('/[0-9]+\.[0-9]/',exec("top -n 2 -d 0.5| grep 'Cpu'"),$cpuArr);
        $cpuId = round($cpuArr[0][3],2);
         var_dump($cpuId);
        echo "\n";
        $_SESSION['cpuId'] = empty($_SESSION['cpuId'])?null:$_SESSION['cpuId'];
        if($_SESSION['cpuId'] != $cpuId) {
            $cpuPercent = 100 - $cpuId;
            $_SESSION['cpuId'] = $cpuId;
            return $cpuPercent;
        }else{
            $_SESSION['cpuId'] = $cpuId;
            return false;
        }
    }

   /**
    *返回内存
    *@reurn string or false 
    */
    public  static function memory() {
        exec('free -h',$commandArr);
        preg_match_all('/[0-9]*M/',$commandArr[1],$arr);
        $_SESSION['memory'] = !empty($_SESSION['memory'])?$_SESSION['memory']:null;
        $memory =  $arr[0][1].'/'.$arr[0][3];
        if($_SESSION['memory'] != $memory) {
            return $memory;
        }else{
            return false;
        }
        $_SESSION['memory'] = $memory;
    }
    

}
